/**********************************************************************/
/*
   Author: Karan Makkar
   Created: June 2024
   Updated: Aug 2025, by Youssef Assarssah
   Description: Prakerja Program and Training Course Take-up in Admin Data
   vs National Surveys report applied/selected. 
   Note: Restrict sample to HH members who took the Susenas survey
   Output: TableA9

*/
/**********************************************************************/

/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
  if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

  clear all
  set more off
  set matsize 11000

* Log
  cap log close
  global prefix: display %tdCYND td(`c(current_date)')
  log using "$KP_logs/${prefix}_report_win_rates_surveytaker.txt", text replace

* Declare filepaths
* Survey-Only
  global mar21 "$KP_deid_susenas/Clean/sus_mar21_deid_clean.dta"
  global sep21 "$KP_deid_susenas/Clean/sus_sep21_deid_clean.dta"
  global mar22 "$KP_deid_susenas/Clean/sus_mar22_deid_clean.dta"
  
  * Merged with Admin
  global mar21_m "$KP_deid_susenas/Clean/sus_mar21_deid_clean_merged.dta"
  global sep21_m "$KP_deid_susenas/Clean/sus_sep21_deid_clean_merged.dta"
  global mar22_m "$KP_deid_susenas/Clean/sus_mar22_deid_clean_merged.dta"

  * SAMPLE RESTRICTIONS: restrict to person-batch observations before date surveyed
  global sus_mar21_precomma inrange(batch, 2, 11) & sus_round == 6
  global sus_sep21_precomma inrange(batch, 2, 18) & sus_round == 7
  global sus_mar22_precomma inrange(batch, 2, 22) & sus_round == 8
  global sus_stack_precomma (inrange(batch, 2, 5) & sus_round == 5) | (inrange(batch, 2, 11) & sus_round == 6) | (inrange(batch, 2, 18) & sus_round == 7) | (inrange(batch, 2, 22) & sus_round == 8)

* SE CLUSTERING (OLS): Individual level
  global vce vce(cluster anon_id4)
  global cluster cluster(anon_id4)

* declare list to keep for first stage regs
    global keep_list anon_id4 ///
                    strata ///
                    gender ///
                    female ///
                    batch /// 
                    urban /// 
                    school_years ///
                    education ///
                    age /// 
                    multi_apply_hh /// 
                    ever_win_3 ///
                    ever_win_5 ///
                    ever_win_11 ///
                    ever_win_17 ///
                    ever_win_18 /// 
                    ever_win_22 ///
                    ever_win_39 ///
                    win_in_batch ///
                    stratum_win_prob

/*----------------------------------------------------*/
* Section:  First Stage Regressions
/*----------------------------------------------------*/

* load susenas
  u "${mar21_m}", clear
  append using "${sep21_m}"
  append using "${mar22_m}"

  keep $keep_list sus_round get_pk hh_pk_win surveytaker
  mdesc $keep_list sus_round get_pk hh_pk_win surveytaker

******* Gen Variables ********

  gen win_before_survey =.
  replace win_before_survey  = ever_win_11 if sus_round == 6 & inrange(batch, 2, 11)
  replace win_before_survey  = ever_win_18 if sus_round == 7 & inrange(batch, 2, 18)
  replace win_before_survey  = ever_win_22 if sus_round == 8 & inrange(batch, 2, 22)

* Report win SUS rename 
  gen report_selected = .
  replace report_selected = get_pk if inlist(sus_round, 5, 8)
  replace report_selected = hh_pk_win if inlist(sus_round, 6, 7)

  *Education cat
  recode education (1/2 = 1) (3 = 2) (4/8 = 3), gen(ed_cat) 

  *Age Cat
  cap drop age_cat
  recode age (0/30 = 1) (31/50 = 2) (51/100 = 3), gen(age_cat) 

  * Drop non-randomized batches
  drop if inlist(batch, 1, 15)

  * Keep only surveytakers
  keep if surveytaker ==1

* Initialize vars
    gen b_fs =.
    gen se_fs = .
    gen b_course =.
    gen se_course =.

* PMO first stage/course
  local i = 1
  foreach wave in sus_stack sus_mar21 sus_sep21 sus_mar22 {

    if "`wave'" != "sus_stack" {
      global absorb absorb(c.stratum_win_prob#batch gender#batch urban#batch ed_cat#batch)
    }
    else {
      global absorb absorb(c.stratum_win_prob#batch#sus_round gender#batch#sus_round urban#batch#sus_round ed_cat#batch#sus_round)
    }

    * Report Selected IV
    qui sum report_selected if ${`wave'_precomma} & win_before_survey == 0
    scalar mean = round(r(mean), 0.001)
    eststo t`i'b: ivreghdfe report_selected (win_before_survey = win_in_batch)  if ${`wave'_precomma}, $absorb $cluster
    estadd scalar control_mean = mean : t`i'b
    estadd scalar obs = e(N) : t`i'b

    * First Stage
    qui sum win_before_survey if ${`wave'_precomma} & win_in_batch == 0
    scalar mean = round(r(mean), 0.001)
    eststo t`i'd: reghdfe win_before_survey win_in_batch  if ${`wave'_precomma}, $absorb $vce
    estadd scalar control_mean = mean : t`i'd
    estadd scalar obs = e(N) : t`i'd
    local i = `i' + 1
  }

/*----------------------------------------------------*/
* Section:  Generate Table
/*----------------------------------------------------*/

*Labels
label var win_before_survey "Ever Win"
label var report_selected "Report Won Kartu Prakerja"
label var win_in_batch "Win In Batch"

* Make Table

*Win in PMO OLS
  esttab t1d t2d t3d t4d using $KP_output/tables/descriptive/tex/TableA9.tex, ///
    mtitle("Stacked" "Mar '21" "Sep '21" "Mar '22" ) ///
    mgroups("Welfare Survey Wave", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
    b(%9.3f) se(%9.3f) ///
    keep(win_in_batch) ///
    collabels(none) ///
    booktabs ///
    label ///
    star(* 0.10 ** 0.05 *** 0.01) /// 
    nolines prehead(`"\hline \hline"') ///
    prefoot(`"\\"') ///
    posthead(`"& (1) & (2) & (3) & (4) \\"' `"\hline \\ \multicolumn{4}{l}{\textit{Panel A: Win in administrative data}} \\\\[-2.5ex]"') ///
    nonum ///
    stats(obs control_mean, label("Observations" "Mean (Control)") fmt(%9.0fc 3)) ///
    fragment ///
    replace

*Report Win IV
  esttab t1b t2b t3b t4b using $KP_output/tables/descriptive/tex/TableA9.tex, ///
    b(%9.3f) se(%9.3f) ///
    keep(win_before_survey) ///
    collabels(none) ///
    booktabs ///
    label ///
    star(* 0.10 ** 0.05 *** 0.01) /// 
    nomtitles nonumbers ///
    prefoot(`"\\"') ///
    nolines posthead("\hline \\ \multicolumn{4}{l}{\textit{Panel B: Report selected in survey data}} \\\\[-2.5ex]") ///
    postfoot(`"Batches & 2-14, 16-22 & 2-11 & 2-14, 16-18 & 2-14, 16-22 \\"' `"\hline"') ///
    stats(obs control_mean, label("Observations" "Mean (Control)") fmt(%9.0fc 3)) ///
    fragment ///
    append

// END